package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;

import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicOperatorTypeDTO;
import com.iwomy.secureplat.platform.entity.BasicDeviceModelEntity;
import com.iwomy.secureplat.platform.entity.BasicOperatorTypeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.BasicDeviceModelService;
import com.iwomy.secureplat.platform.service.BasicOperatorTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 运营商表
 *
 * @author pig
 * @date 2023-11-13 18:04:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/basicOperatorType")
@Tag(description = "basicOperatorType", name = "运营商表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BasicOperatorTypeController {

	private final BasicOperatorTypeService basicOperatorTypeService;

	private final BasicDeviceModelService basicDeviceModelService;

	/**
	 * 分页查询
	 *
	 * @param page                 分页对象
	 * @param basicOperatorTypeDTO 运营商表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_view')")
	public R getBasicOperatorTypePage(@ParameterObject Page page, @ParameterObject BasicOperatorTypeDTO basicOperatorTypeDTO) {
		return R.ok(basicOperatorTypeService.getBasicOperatorTypePage(page, basicOperatorTypeDTO));
	}

	/**
	 * 分页查询
	 *
	 * @param basicOperatorType 设备类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	public R getBasicOperatorTypeList(@ParameterObject BasicOperatorTypeEntity basicOperatorType) {
		return R.ok(basicOperatorTypeService.pageByScope(basicOperatorType));
	}


	/**
	 * 通过id查询运营商表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(basicOperatorTypeService.getById(id));
	}

	/**
	 * 新增运营商表
	 *
	 * @param basicOperatorType 运营商表
	 * @return R
	 */
	@Operation(summary = "新增运营商表", description = "新增运营商表")
	@SysLog("新增运营商表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_add')")
	public R save(@RequestBody BasicOperatorTypeEntity basicOperatorType) {
		BasicOperatorTypeEntity wrapperEntity = new BasicOperatorTypeEntity();
		wrapperEntity.setName(basicOperatorType.getName());
		LambdaQueryWrapper<BasicOperatorTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicOperatorTypeEntity one = basicOperatorTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one)) {
//			throw new PlatformException("运营商已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.OPERATOR_ID_EXIST));
		}
		basicOperatorType.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(basicOperatorTypeService.save(basicOperatorType));
	}

	/**
	 * 修改运营商表
	 *
	 * @param basicOperatorType 运营商表
	 * @return R
	 */
	@Operation(summary = "修改运营商表", description = "修改运营商表")
	@SysLog("修改运营商表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_edit')")
	public R updateById(@RequestBody BasicOperatorTypeEntity basicOperatorType) {
		long count = basicDeviceModelService
				.count(Wrappers.lambdaQuery(new BasicDeviceModelEntity()).eq(BasicDeviceModelEntity::getOperatorId, basicOperatorType.getId()));

		if (count > 0) {
//			return R.failed("运营商已被使用不允许修改");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.OPERATOR_ID_USED_MODIFY));
		}

		BasicOperatorTypeEntity wrapperEntity = new BasicOperatorTypeEntity();
		wrapperEntity.setName(basicOperatorType.getName());
		LambdaQueryWrapper<BasicOperatorTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		BasicOperatorTypeEntity one = basicOperatorTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(basicOperatorType.getId())) {
//			throw new PlatformException("运营商已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.OPERATOR_ID_EXIST));
		}
		return R.ok(basicOperatorTypeService.updateById(basicOperatorType));
	}


	/**
	 * 启用禁用
	 *
	 * @param basicOperatorType 运营商
	 * @return R
	 */
	@Operation(summary = "启用禁用运营商", description = "启用禁用运营商")
	@SysLog(expression = "'启用禁用运营商'.concat(#basicOperatorType.remarks)")
	@PutMapping("/enableOrDisable")
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_enableOrDisable')")
	public R enableOrDisable(@RequestBody BasicOperatorTypeDTO basicOperatorType) {
		BasicOperatorTypeEntity basicOperatorTypeEntity = new BasicOperatorTypeEntity();
		basicOperatorTypeEntity.setId(basicOperatorType.getId());
		basicOperatorTypeEntity.setRemarks(basicOperatorType.getRemarks());
		basicOperatorTypeEntity.setState(basicOperatorType.getState());
		return R.ok(basicOperatorTypeService.updateById(basicOperatorTypeEntity));
	}

	/**
	 * 通过id删除运营商表
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除运营商表", description = "通过id删除运营商表")
	@SysLog("通过id删除运营商表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_del')")
	public R removeById(@RequestBody Long[] ids) {

		long count = basicDeviceModelService.count(Wrappers.<BasicDeviceModelEntity>lambdaQuery().in(BasicDeviceModelEntity::getOperatorId, ids));

		if (count > 0)
//			return R.failed("运营商已被使用不允许删除");
			return R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.OPERATOR_ID_USED_DELETE));

		return R.ok(basicOperatorTypeService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param basicOperatorType 查询条件
	 * @param ids               导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_basicOperatorType_export')")
	public List<BasicOperatorTypeEntity> export(BasicOperatorTypeEntity basicOperatorType, Long[] ids) {
		return basicOperatorTypeService.list(Wrappers.lambdaQuery(basicOperatorType).in(ArrayUtil.isNotEmpty(ids), BasicOperatorTypeEntity::getId, ids));
	}
}